import java.util.*;
import sdljava.SDLException;
import sdljava.event.*;
import sdljava.ttf.*;
import sdljava.video.*;
import sdljavax.gfx.SDLGfx;

public class Input {
	private SDLSurface screen;
	private SDLRect textOffset = new SDLRect();
	private Vector<ButtonSprite> buttons;
	private Vector<Chosen> userAnswer;
	private int Amount = 0;
	private SiteMaterials selectedMaterial;
	private GameWindow gameWindow;
	private int horizontal = 1;
	private int vertical = 0;
	
	private String indata = "";
	private SDLSurface input;
	private SDLTrueTypeFont font = SDLTTF.openFont("fonts/digital.ttf", 72);
	private SDLColor color = new SDLColor();
	
	public Input(GameWindow gameWindow, Vector<Chosen> userAnswer, String imageFile, 
			SiteMaterials material) throws SDLException{
		this.gameWindow = gameWindow;
		this.userAnswer = userAnswer;
		this.screen = loadfile(imageFile);
		this.selectedMaterial = material;
		buttons = new Vector<ButtonSprite>();
		populateSVector();
		font = SDLTTF.openFont("fonts/digital.ttf", 72);
		textOffset.setX(390);
		textOffset.setY(315);
		color = new SDLColor();
		color.setBlue(0);
		color.setGreen(0);
		color.setRed(255);

	}
	public void populateSVector() throws SDLException{
		ImageData Okaydimensions = new ImageData(2, horizontal, 363, 113, 0, 0);
		Okaydimensions.setZoom(0.5, 0.5);
		ImageData Canceldimensions = new ImageData(2, horizontal, 363, 113, 0, 113);
		Canceldimensions.setZoom(0.5, 0.5);
		
		ButtonSprite tempButton = new ButtonSprite(gameWindow, Okaydimensions, 400, 400, "bitmaps/popup_button_sprite.bmp");
		buttons.add(tempButton);
		tempButton = new ButtonSprite(gameWindow, Canceldimensions, 600, 400, "bitmaps/popup_button_sprite.bmp");
		buttons.add(tempButton);
	}
	public SDLSurface loadfile(String filename) throws SDLException{
		SDLSurface loadedImage = null;
		loadedImage = SDLVideo.loadBMP(filename);
		SDLSurface optimizedImage = loadedImage.displayFormat();
		loadedImage.freeSurface();
		optimizedImage = SDLGfx.zoomSurface(optimizedImage, 0.2, 0.2, false);
		return optimizedImage;
	}
	public void show() throws SDLException{
		SDLRect offset = new SDLRect();
		
		offset.setX(300);
		offset.setY(200);
		
		screen.blitSurface(gameWindow.getGameSurface(), offset);
		gameWindow.getGameSurface().flip();
		for(int i = 0; i < buttons.size(); i++){
			buttons.get(i).show(buttons.get(i).getSpriteImage().get(0));
		}
	}
	public Boolean handleEvent(SDLEvent event) throws SDLException{
		Boolean close = false;
		if(event.getType() == SDLEvent.SDL_MOUSEMOTION){
			for(int i = 0; i < buttons.size(); i++){
				buttons.get(i).spriteHandle(event);
			}
		}
		if(event.getType() == SDLEvent.SDL_MOUSEBUTTONDOWN){
				for(int i = 0; i < buttons.size(); i++){
					if(buttons.get(i).spriteHandle(event) == true){
						if(i == 0){
							if(userAnswer.size() == 0){
								Chosen selected = new Chosen();
								selected.setAmount(Amount);
								selected.setChosenMaterial(selectedMaterial.getMaterial());
								userAnswer.add(selected);
							}
							else{
								for(int j = 0; j < userAnswer.size(); j++){
									if(userAnswer.get(j).getChosenMaterial() == selectedMaterial.getMaterial()){
										userAnswer.get(j).setAmount(userAnswer.get(j).getAmount()+ Amount);
									}
									else{
										Chosen selected = new Chosen();
										selected.setAmount(Amount);
										selected.setChosenMaterial(selectedMaterial.getMaterial());
										userAnswer.add(selected);
									}
								}
							}
							selectedMaterial.setUsed(selectedMaterial.getUsed()+Amount);
							close = true;
						}
						if(i == 1){
							close = true;
						}
					}
				}
			}

		if(event.getType() == SDLEvent.SDL_KEYDOWN){
			int data = ((SDLKeyboardEvent)event).getSym() - 48;
			if(data >= 0 && data <= 9){
					String newin = indata + Integer.toString(data);
					Integer newamount = Integer.decode(newin);
				if(newamount <= selectedMaterial.getAvailableAmount() - selectedMaterial.getUsedAmount()){
					indata += Integer.toString(data);
					Amount = Integer.decode(indata);
					input = font.renderTextBlended(indata, color);
					input.blitSurface(gameWindow.getGameSurface(),textOffset);
					gameWindow.getGameSurface().flip();
				}
			}else if(data == -40)
				if(indata.length()-1 > -1){
					indata = String.copyValueOf(indata.toCharArray(), 0, indata.length()-1);
					if(indata.length() == 0){
						show();
						indata = "";
						Amount = 0;
					}
					else
						Amount = Integer.decode(indata);
				}
				if(indata.length() > 0){
					input = font.renderTextBlended(indata, color);
					show();
					input.blitSurface(gameWindow.getGameSurface(),textOffset);
					gameWindow.getGameSurface().flip();
				}
			}
		return close;
	}
}
